package com.ysuedu.dao;

import com.ysuedu.common.DBUtils;
import com.ysuedu.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class UserDao {
    public boolean insert(User user) {
        String sql = "INSERT INTO user (UserAccount,UserName,UserPassword,identity,createDate) VALUES(?,?,?,?,?)";
        try {
            Connection connection = DBUtils.getConnection();
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUserAccount());
            ps.setString(2, user.getUserName());
            ps.setString(3, user.getUserPwd());
            ps.setInt(4,user.getIdentity());
            //数据类型转换，将java.util.Date 转为java.sql.Date
            ps.setDate(5, new java.sql.Date(user.getCreateDate().getTime()));
            if (ps.executeUpdate() > 0) {
                DBUtils.close(connection, ps, null);
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    //检查注册时用户的账号（手机号）是否重复
    public boolean checkAccountValid(String account){
        String sql="SELECT * FROM USER WHERE userAccount=?";
        try{
            Connection connection=DBUtils.getConnection();
            PreparedStatement ps= connection.prepareStatement(sql);
            ps.setString(1,account);
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                System.out.println(rs.getString("UserName"));
                DBUtils.close(connection,ps,rs);
                return false;
            }
            DBUtils.close(connection,ps,rs);
            return true;
        }catch (Exception e){
            e.getStackTrace();
            return false;
        }
    }
    public User findUserByAccAndPwd(String account,String pwd){
        String sql="SELECT * FROM user WHERE userAccount=? AND userPassword=?";
        try{
            Connection connection=DBUtils.getConnection();
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,account);
            ps.setString(2,pwd);
            ResultSet rs=ps.executeQuery();
            User user=new User();
            if(rs.next()){
                user.setUserAccount(rs.getString("UserAccount"));
                user.setUserName(rs.getString("UserName"));
                //user.setUserPwd(rs.getString("UserPassword")); //密码暂时不存储
                user.setUserId(rs.getInt("UserId"));
                user.setAddress(rs.getString("address"));
                user.setIdentity(rs.getInt("identity"));
                user.setAvatar(rs.getString("avatar"));
                user.setCreateDate(rs.getDate("createDate"));
                DBUtils.close(connection,ps,rs);
                return user;
            }
        }catch (Exception e){
            e.getStackTrace();
            return null;
        }
        return null;
    }

    public static void main(String[] args){


    }
}
